package com.example.firstdemo.ui.screens.pages.addProblem.addProblem

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.firstdemo.network.api.addProblem.AddProblemApi
import com.example.firstdemo.network.service.NetworkException
import com.example.firstdemo.ui.screens.pages.addProblem.addProblem.addDescription.AddDescriptionEvent
import com.example.firstdemo.ui.screens.pages.addProblem.addProblem.data.AddProblemUIState
import com.example.firstdemo.ui.screens.pages.addProblem.addProblem.selectCarriage.SelectCarriageEvent
import com.example.firstdemo.ui.screens.pages.addProblem.addProblem.selectPosition.SelectPositionEvent
import com.example.firstdemo.ui.screens.pages.addProblem.addProblem.selectProblem.SelectProblemEvent
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

class AddProblemViewModel : ViewModel() {
    private val _uiState = MutableStateFlow(AddProblemUIState())
    val uiState: StateFlow<AddProblemUIState> = _uiState

    init {
        viewModelScope.launch {
            SelectCarriageEvent.vehicle.collect {
                _uiState.value = _uiState.value.copy(
                    vehicleData = it
                )
            }
        }
        viewModelScope.launch {
            SelectPositionEvent.position.collect {
                _uiState.value = _uiState.value.copy(
                    positionData = it
                )
            }
        }
        viewModelScope.launch {
            SelectProblemEvent.problem.collect {
                _uiState.value = _uiState.value.copy(
                    problemDictData = it
                )
            }
        }
        viewModelScope.launch {
            AddDescriptionEvent.description.collect {
                _uiState.value = _uiState.value.copy(
                    descriptionData = it
                )
            }
        }
    }

    fun resetUIState() {
        _uiState.value = _uiState.value.copy(
            vehicleData = null,
            positionData = null,
            dialogShow = false
        )
    }

    suspend fun save(filePath: String) {
        val vehicleData = uiState.value.vehicleData!!
        val positionData = uiState.value.positionData!!
        val problemData = uiState.value.problemDictData!!
        val descriptionData = uiState.value.descriptionData
        try {
            AddProblemApi.addProblem(
                stationId = vehicleData.stationId,
                passTime = vehicleData.passTime,
                carriageOrder = vehicleData.carriage.carriageOrder,
                carriageUid = vehicleData.carriage.uid,
                vehicleUid = vehicleData.carriage.vehicleUid,
                carriageId = vehicleData.carriage.id,
                indexId = vehicleData.indexId,
                vehicleId = vehicleData.id,
                vehicleNumber = vehicleData.vehicleNumber,
                carriageNo = vehicleData.carriage.carriageNo,
                position = positionData.value,
                remarks = problemData.label,
                remarksCode = problemData.code,
                confirmSuggestion = descriptionData,

                filePath = filePath,
                filename = "imageData"
            )
        } catch (e: NetworkException) {
        }
    }
}